package com.et114.components.simpletable;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class EnFooter extends DefaultFooter {

	public EnFooter ( DataList pageInfo , String formName , String showSummary ) {
		super( pageInfo, formName, showSummary );
	}

	/**
	 * 获得查询字符串，为了保持原有的查询条件信息不丢失
	 * 
	 * @author chengjiangz
	 * @return String
	 */
	public String getQueryStringOnUri ( ) {
		Set set = new HashSet();
		StringBuffer sb = new StringBuffer();
		Map addininfo = pageInfo.getAddinInfo();
		set.addAll( addininfo.keySet() );

		for ( Iterator iter = set.iterator(); iter.hasNext(); ) {
			String key = ( String ) iter.next();
			String value = ( String ) addininfo.get( key );

			if ( value != null && !value.equals( "" ) ) {
				sb.append( key + "=" + value ).append( "&" );
			}
		}
		String retValue = sb.toString();
		if ( retValue.matches( ".*\\&$" ) )
			retValue = retValue.substring( 0, retValue.length() - 1 );
		return retValue;

	}

	/**
	 * 只有数字
	 * 
	 * @return
	 */
	public String getOnlyRedirect ( ) {
		StringBuffer footer = new StringBuffer();
		footer.append( this.getJavascript() );
		String spanbegin = "<span class=\"simpletablespan\">" + getSummaryInfo()
				+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		String spanend = "</span>";
		footer.append( spanbegin );
		if ( pageInfo.getMaxPage() > 1 ) {
			footer.append( getDirectPageCode() );
		}
		footer.append( spanend );
		return footer.toString();
	}

	/**
	 * 没有数字
	 * 
	 * @return
	 */
	public String getSimple ( ) {
		StringBuffer footer = new StringBuffer();
		footer.append( this.getJavascript() );
		String prev = getPrevHref( true );
		String next = getNextHref( true );
		String first = getFirstHref( true );
		String last = getLastHref( true );
		String spanbegin = "<span class=\"simpletablespan\">" + getSummaryInfo()
				+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		String spanend = "</span>";
		footer.append( spanbegin );
		if ( pageInfo.getMaxPage() > 1 ) {
			footer.append( first );
			footer.append( prev );
			footer.append( next );
			footer.append( last );
		}
		footer.append( spanend );
		return footer.toString();
	}

	/**
	 * 只有前一页和后一页
	 * 
	 * @return
	 */
	public String getOnlyNext ( ) {
		StringBuffer footer = new StringBuffer();
		footer.append( this.getJavascript() );

		String prev = getPrevHref( false );
		String next = getNextHref( false );
		String spanbegin = "<span class=\"simpletablespan\">" + getSummaryInfo()
				+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		String spanend = "</span>";
		footer.append( spanbegin );
		if ( pageInfo.getMaxPage() > 1 ) {
			footer.append( prev );
			footer.append( next );
		}
		footer.append( spanend );
		return footer.toString();
	}

	/**
	 * 首页 上一页 数字 下一页 末页
	 * 
	 * @return
	 */
	public String getNormal ( ) {
		StringBuffer footer = new StringBuffer();
		footer.append( this.getJavascript() );
		String first = getFirstHref( true );
		String last = getLastHref( true );
		String prev = getPrevHref( true );
		String next = getNextHref( true );
		String spanbegin = "<span class=\"simpletablespan\">" + getSummaryInfo()
				+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		String gotoPage = getGotoPageControl();
		String spanend = "</span>";
		footer.append( spanbegin );
		if ( pageInfo.getMaxPage() > 1 ) {
			footer.append( first );
			footer.append( prev );
			footer.append( getDirectPageCode() );
			footer.append( next );
			footer.append( last );
			footer.append( "&nbsp;&nbsp;" ).append( gotoPage );
		}
		footer.append( spanend ).append( "<br>" );
		return footer.toString();
	}

	public String getGotoPageControl ( ) {
		String gotoPage = "\t<input name=\"gotoPage"
				+ "\" id=\"gotoPage"
				+ "\" type=\"text\" size=\"3\" maxlength=\"9\" title=\"please input page number\">\n"
				+ "\t<input type=\"button\" name=\"gotoButton"
				+ "\" id=\"gotoButton" + "\" onClick=\"changeurl2"
				+ "();\" value=\"go\" class=\"simpletableGoButton\">";
		return gotoPage;
	}

	public String getLastHref ( boolean show ) {
//		String last = pageInfo.getUri() + "?" + pageInfo.getPageKey() + "="
//				+ pageInfo.getMaxPage() + "&" + getQueryStringOnUri();
		int currentPage = pageInfo.getCurrentPage();
		int maxPage = pageInfo.getMaxPage();

		return currentPage == maxPage ? ( show ? " [Last] " : "" )
				: " <a class=\"simpletableHeaf\" href=\"#\" onClick=\"go(" + pageInfo.getMaxPage()
						+ ")\">[Last]</a> ";
	}

	public String getNextHref ( boolean show ) {
//		String next = pageInfo.getUri() + "?" + pageInfo.getPageKey() + "="
//				+ pageInfo.getNextPage() + "&" + getQueryStringOnUri();
		int currentPage = pageInfo.getCurrentPage();
		int maxPage = pageInfo.getMaxPage();
		return currentPage == maxPage ? ( show ? " [Next] " : "" )
				: " <a class=\"simpletableHeaf\" href=\"#\" onClick=\"go(" + pageInfo.getNextPage()
						+ ")\">[Next]</a> ";
	}

	public String getPrevHref ( boolean show ) {
//		String prev = pageInfo.getUri() + "?" + pageInfo.getPageKey() + "="
//				+ pageInfo.getPrevPage() + "&" + getQueryStringOnUri();
		int currentPage = pageInfo.getCurrentPage();
		return currentPage == 1 ? ( show ? " [Previous] " : "" )
				: " <a class=\"simpletableHeaf\" href=\"#\" onClick=\"go(" + pageInfo.getPrevPage()
						+ ")\">[Previous]</a> ";

	}

	public String getFirstHref ( boolean show ) {
//		String first = pageInfo.getUri() + "?" + pageInfo.getPageKey() + "=1"
//				+ "&" + getQueryStringOnUri();
		int currentPage = pageInfo.getCurrentPage();

		String firsthref = currentPage == 1 ? ( show ? " [First] " : "" )
				: " <a class=\"simpletableHeaf\" href=\"#\" onClick=\"go(1)\">[First]</a> ";

		return firsthref;
	}

	public String getSummaryInfo ( ) {
		if ( showSummary != null && showSummary.equals( "false" ) )
			return "";
		int pageSize = pageInfo.getPageSize();
		int currentPage = pageInfo.getCurrentPage();
		int maxPage = pageInfo.getMaxPage();
		int maxCount = pageInfo.getMaxCount();
		String scope = ( pageSize * ( currentPage - 1 ) + 1 )
				+ "~"
				+ ( pageSize * currentPage >= maxCount ? maxCount : pageSize
						* currentPage );
		
		return new StringBuffer()./*append( "the" ).append( currentPage ).append(
				"page total" ).append( maxPage ).append( "page;(sum" ).append(
				maxCount ).append( " records);" ).*/append( scope ).toString();

	}

	/**
	 * 带数字页码
	 * 
	 * @return
	 */
	public String getDirectPageCode ( ) {
		String str = "";
		int currentPage = pageInfo.getCurrentPage();
		int maxPage = pageInfo.getMaxPage();
		int begin = ( currentPage - 4 ) < 1 ? 1 : ( currentPage - 4 );
		int end = ( currentPage + 4 ) > maxPage ? maxPage : ( currentPage + 4 );
		if ( end - begin < 9 ) {
			end = begin + 9 < maxPage ? begin + 9 : maxPage;
			begin = end - 9 > 1 ? end - 9 : 1;
		}

		StringBuffer sb = new StringBuffer();

		for ( int i = begin; i <= end; i++ ) {
//			String next = pageInfo.getUri() + "?" + pageInfo.getPageKey() + "="
//					+ i + "&" + getQueryStringOnUri();

			String directhref = " <a class=\"simpletableHeaf\" href=\"" + "#" + "\" onClick=\"go(" + i
					+ ")\" >" + i + "</a> ";

			if ( currentPage == i )
				directhref = "<strong>[" + String.valueOf( i ) + "]</strong>";
			sb.append( directhref );
		}
		str = sb.toString();

		if ( end != maxPage ) {
			str = str + "...";
		}
		if ( begin != 1 ) {
			str = "..." + str;
		}

		return str;
	}

	public String getJavascript ( ) {
		StringBuffer sb = new StringBuffer();
		String pathname = pageInfo.getUri();
		sb.append( "\n<script language=\"javascript\">\n"
						+ "\t var maxPage="
						+ pageInfo.getMaxPage()
						+ ";\n"
						+ "\tfunction isDigit(s)\n"
						+ "\t{\n"
						+ "\tvar patrn=/^[0-9]{1,20}$/;\n"
						+ "\tif (!patrn.exec(s)) return false\n"
						+ "\treturn true\n"
						+ "\t}\n"
						+ "\tfunction isNumberFloat(inputString)\n"
						+ "\t{\n"
						+ "\t  return (!isNaN(parseInt(inputString))) ? true : false;\n"
						+ "\t}\n"
						+ "\t function changeurl"
						+ "()\n"
						+ "\t{\n"
						+ "\t\tvar pageNo = document.getElementById(\"gotoPage"
						+ "\").value;\n"
						+ "\t\tif(isDigit(pageNo) && isNumberFloat(pageNo))\n"
						+ "\t\t{\n"
						+ "\t\t\tif(pageNo>maxPage){\n"
						+ "\t\t\t\talert(\"overstep max pages/\"+maxPage+\"page！\");\n"
						+ "\t\t\t\tdocument.getElementById(\"gotoPage"
						+ "\").focus();\n"
						+ " \t\t\t\treturn;\n"
						+ " \t\t\t}\n"
						+ "\t\t\twindow.location.href ='"
						+ pathname
						+ "?"
						+ pageInfo.getPageKey()
						+ "='+pageNo+'&"
						+ getQueryStringOnUri()
						+ "';\n"
						+ "\t\t}\n"
						+ "\t\telse\n"
						+ "\t\t{\n"
						+ "\t\t\talert(\"please input integer!\");\n"
						+ "\t\t}\n" + "\t}\n"

						+ this.getJavascript2()

						+ "</script>\n" );
		return sb.toString();
	}

	public String getJavascript2 ( ) {
		StringBuffer sb = new StringBuffer();
		sb.append( " function go(pageNum){   \n" );
		sb.append( "	  var actionurl = document." + this.formName + ".action; \n" );
		sb.append( "     if(actionurl.indexOf(\"?\")==-1) actionurl = actionurl+\"?\";\n" );
		sb.append( "	    document." + this.formName + ".action=actionurl+\"&pageNum=\"+pageNum;\n" );
		sb.append( "	    document." + this.formName + ".submit(); \n" );
		sb.append( "	 } \n" );

		sb.append( "	function changeurl2()\n" );
		sb.append( "		{ " );
		sb.append( "			var pageNo = document.getElementById(\"gotoPage\").value; \n" );
		// sb.append(" alert(pageNo)\n");
		sb.append( "			if(isDigit(pageNo) && isNumberFloat(pageNo)){\n" );

		sb.append( "	      		if(pageNo>maxPage){\n" );
		sb.append( "		    	    alert(\"overstep max pages/\"+maxPage+\"page\");\n" );
		sb.append( "		        	document.getElementById(\"gotoPage\").focus();\n" );
		sb.append( " 			   return;\n" );

		sb.append( " 	        }\n" );
		sb.append( " 	        go(pageNo) \n" );

		sb.append( " 	    }else{\n" );
		sb.append( "			   alert(\"please input integer!\");\n" );
		sb.append( "		     }\n" );
		sb.append( "  }\n" );

		return sb.toString();
	}

}
